package cn.jiyun.controller;

import cn.jiyun.constant.MessageConstant;
import cn.jiyun.entity.Result;
import cn.jiyun.pojo.HotSetmeal;
import cn.jiyun.pojo.ReportData;
import cn.jiyun.serivce.ReportService;
import cn.jiyun.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("report")
public class ReportController {

    @Reference
    private ReportService reportService;

    @RequestMapping("getMemberReport")
    public Result getMemberReport(){
        try {

            //获取当前日期的日历实例
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,-12);//获取当前日期的前12个月的日期
            List<String> xdataList = new ArrayList<>();
            List<Integer> ydataList = new ArrayList<>();
            for(int i = 0;i<12;i++){
                calendar.add(Calendar.MONTH,1);
                String xdata = new SimpleDateFormat("yyyy.MM").format(calendar.getTime());
                xdataList.add(xdata);
                String regDate = xdata+".31";
//                SELECT COUNT(*) FROM t_member WHERE regTime <= '2020-10-31'
                Integer ydata = reportService.getCountByRegDate(regDate);
                ydataList.add(ydata);
            }

            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("xdata",xdataList);
            resultMap.put("ydata",ydataList);

            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("getSetmealReport")
    public Result getSetmealReport(){
        try {

            List<Map<String,Object>> data = reportService.getSetmealReport();

            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @RequestMapping("getBusinessReportData")
    public Result getBusinessReportData(){
        try {

            ReportData reportData = reportService.getBusinessReportData();

            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,reportData);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @RequestMapping("exportBusinessReport")
    public void exportBusinessReport(HttpServletRequest request, HttpServletResponse response){
        try {

            //1、查询数据
            ReportData reportData = reportService.getBusinessReportData();

            String path = request.getSession().getServletContext().getRealPath("/template/report_template.xlsx");
            //创建工作簿
            Workbook workbook = WorkbookFactory.create(new File(path));
            //获取sheet表
            Sheet sheet = workbook.getSheetAt(0);

            sheet.getRow(2).getCell(5).setCellValue(reportData.getReportDate());


            sheet.getRow(4).getCell(5).setCellValue(reportData.getTodayNewMember());
            sheet.getRow(4).getCell(7).setCellValue(reportData.getTotalMember());

            sheet.getRow(5).getCell(5).setCellValue(reportData.getThisWeekNewMember());
            sheet.getRow(5).getCell(7).setCellValue(reportData.getThisMonthNewMember());

            sheet.getRow(7).getCell(5).setCellValue(reportData.getTodayOrderNumber());
            sheet.getRow(7).getCell(7).setCellValue(reportData.getTodayVisitsNumber());

            sheet.getRow(8).getCell(5).setCellValue(reportData.getThisWeekOrderNumber());
            sheet.getRow(8).getCell(7).setCellValue(reportData.getThisWeekVisitsNumber());

            sheet.getRow(9).getCell(5).setCellValue(reportData.getThisMonthOrderNumber());
            sheet.getRow(9).getCell(7).setCellValue(reportData.getThisMonthVisitsNumber());

            List<HotSetmeal> hotSetmeal = reportData.getHotSetmeal();//热门套餐
            for (int i = 0; i <hotSetmeal.size() ; i++) {
                HotSetmeal hotSetmeal1 = hotSetmeal.get(i);
                Row row = sheet.getRow(i + 12);
                row.getCell(4).setCellValue(hotSetmeal1.getName());
                row.getCell(5).setCellValue(hotSetmeal1.getSetmeal_count());
                row.getCell(6).setCellValue(hotSetmeal1.getProportion()*100+"%");
                row.getCell(7).setCellValue(hotSetmeal1.getRemark());
            }

            //下载
            //使用输出流进行表格下载,基于浏览器作为客户端下载
            OutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");//代表的是Excel文件类型
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");//指定以附件形式进行下载
            workbook.write(out);
            out.flush();
            out.close();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
